import { message } from "ant-design-vue";
import { Abstract } from "./abstract";

const http: Record<string, any> = new Abstract();

export interface HttpResponse {
  status: number;
  message: string;
  data: () => Record<string, unknown>;
}

export { http };
export default ["getReq", "postReq"].reduce<{
  [method: string]: (params?: any) => Promise<any>;
}>((obj, method) => {
  obj[method] = (requestConfig?: any) =>
    http[method]?.(requestConfig).then(
      (res?: { body?: any; message?: string; status?: number }) => {
        if (res?.status === 0) {
          return res;
        } else {
          if (res?.message) {
            message.error(res.message);
          }
          throw new Error(res?.message || requestConfig.url + "请求失败");
        }
      }
    );
  return obj;
}, {});
// export default class http {
//   static getReq(params: any) {
//     console.log(params);
//     return new Promise((resolve) => {
//       setTimeout(() => {
//         resolve("");
//       }, 1000);
//     });
//   }
//   static postReq(params: any) {
//     console.log(params);
//     return new Promise((resolve) => {
//       setTimeout(() => {
//         resolve("");
//       }, 1000);
//     });
//   }
// }
